package proj.unions.general;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.Properties;

import android.app.Activity;

public class Text {

	private static Text mPropertiesData = null;
	private Properties properties = null;
	
	public static Text getInstance()
	{
		if(mPropertiesData == null)
			mPropertiesData = new Text();
		return mPropertiesData;
	}
	
	public Text()
	{
		Activity a = SuperTools.getInstance().getCurActivity();
		InputStream is;
		try {
			String fileName = PropertiesData.getInstance().getString("LOCAL_LANGUAGE");
			if(fileName.equals(""))
				fileName = "zh";
			is = a .getAssets().open(fileName + ".properties");
			properties = new Properties();
			properties.load(is);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public boolean getBoolean(String name) {
		return Boolean.parseBoolean(getString(name));
	}

	public String getString(String name) {
		try {
			return new String(properties.getProperty(name).getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			return properties.getProperty(name);
		}
	}

	public static String getKey(String name) {
		try {
			return new String(getInstance().properties.getProperty(name).getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			return getInstance().properties.getProperty(name);
		}
	}

	public String getString(String name, Object... args) {
		try {
			String str = new String(properties.getProperty(name).getBytes("ISO-8859-1"), "UTF-8");
			str = MessageFormat.format(str, args);
			return str;
		} catch (UnsupportedEncodingException e) {
			return properties.getProperty(name);
		}
	}

	public Integer getInteger(String name) {
		String str = properties.getProperty(name);
		if (name == null)
			return null;
		return Integer.parseInt(str);
		
	}
}
